
package com.yimixian.app.fragment;

import com.yimixian.app.R;
import com.yimixian.app.cart.CartManager;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.rest.api.CartUpdateAPI;
import com.yimixian.app.ui.BaseHttpFragment;
import com.yimixian.app.ui.MessageDialogView;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import android.view.View;
import android.view.ViewGroup;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 用来显示Dialog的BaseFragment Created by songzhiyang on 2015/8/21.
 */
public abstract class DialogGoodDetailFragment extends BaseHttpFragment{
    protected ViewGroup mDialogView;
    protected CartUpdateAPI mCartUpdateAPI;
    protected PlusMinusIcon mPlusMinusIcon;

    protected void showCountLimitDialog() {
        final MessageDialogView messageDialogView = new MessageDialogView(getActivity());
        messageDialogView.bind(getString(R.string.confirm), "亲，每次只支持换购一个品，数量有限，先到先得哦！",
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialogView.removeView(messageDialogView);
                }
            });
        mDialogView.addView(messageDialogView);
    }

    protected void showMoneyLimitDialog(float price) {
        final MessageDialogView messageDialogView = new MessageDialogView(getActivity());
        messageDialogView.bind(getString(R.string.confirm), "选购满 "
                + price + "元就可以点击换购此品啦！数量有限，快快抢购吧！",
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialogView.removeView(messageDialogView);
                }
            });
        mDialogView.addView(messageDialogView);
    }


    public void setDialogView(ViewGroup viewGroup) {
        mDialogView = viewGroup;
    }

    protected void fetchUpdateCartData() {
        fetchData(true, CartUpdateAPI.CART_UPDATE_API);
    }

    protected void initCartUpdateAPI() {
        mCartUpdateAPI = new CartUpdateAPI(this);
    }

    /**
     * 处理 + / - 号网络请求
     */
    protected void dealPlusMinusProtocol() {
        showPopupWindow(getActivity(), R.id.popupwindow_container);
        if(!CartManager.getInstance().isUserLogin()){
//            int count = mPlusMinusIcon.mGoodsItem.count==null?0:Integer.parseInt(mPlusMinusIcon.mGoodsItem.count);
            try {
                if (Constants.PLUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
                    /*if(count+1>mPlusMinusIcon.mGoodsItem.shouldBuyCount){
                        UiUtils.showToast("该商品已达购买上限");
                        return;
                    }*/
                    CartManager.getInstance().saveUnLoginCartData(mPlusMinusIcon.mGoodsItem.id, true, mPlusMinusIcon);
                }else if (Constants.MINUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
                    CartManager.getInstance().saveUnLoginCartData(mPlusMinusIcon.mGoodsItem.id, false,mPlusMinusIcon);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                UiUtils.showToast("数据保存失败");
            }
            hidePopupWindow();
        }else{
            fetchUpdateCartData();
            if(mPlusMinusIcon != null && mCartUpdateAPI != null) {
                mCartUpdateAPI.mGoodId = mPlusMinusIcon.mGoodsItem.id;
                if (Constants.PLUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
                    mCartUpdateAPI.mTrend = 1;
                    getUpdate();
                }
                else if (Constants.MINUS_BUTTON_CLICKED_BROADCAST.equals(mPlusMinusIcon.mButtonState)) {
                    mCartUpdateAPI.mTrend = 0;
                    getUpdate();
                }
            }
        }
    }
    private void getUpdate(){
        if(mPlusMinusIcon.mCurrentButtonState.equals(Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST)){
            mCartUpdateAPI.getCartUpdate(1);
        }else{
            mCartUpdateAPI.getCartUpdate();
        }
    }

    public PlusMinusIcon getmPlusMinusIcon() {
        return mPlusMinusIcon;
    }
}
